package com.example.calendertodo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;
import android.widget.TextView;

import com.example.calendertodo.adapter.MainAdapter;
import com.example.calendertodo.beam.Todo;
import com.example.calendertodo.util.TodoDbOpenHelper;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.haibin.calendarview.WeekViewPager;
import com.haibin.calendarview.YearViewPager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("all")
public class MainActivity extends AppCompatActivity implements CalendarView.OnYearChangeListener{
    private com.haibin.calendarview.CalendarView calendarView;
    private CalendarLayout mCalendarLayout;
    private YearViewPager yearViewPager;
    private WeekViewPager weekViewPager;

    private MainAdapter mMainAdapter;
    private TodoDbOpenHelper mTodoDbOpenHelper;
    private List<Todo> mTodo,nTodo;
    private RecyclerView mRecyclerView;
    private String currentDate, calender_month, calender_day, nowDay;
    private TextView tvClue;
    private SimpleDateFormat simpleDateFormat;
    private Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setTitle(nowDay);
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                int i = calendar.getYear();
                currentDate = i + "-" + format(calendar.getMonth()) + "-" + format(calendar.getDay());
                setTitle(currentDate);
                Log.d("tag", "----" + currentDate);
                mTodo = mTodoDbOpenHelper.queryFromDbByDate(currentDate);
                if (mTodo == null || mTodo.size() == 0) {
                    tvClue.setVisibility(View.VISIBLE);
                } else {
                    tvClue.setVisibility(View.GONE);
                }

                mMainAdapter.refreshData(mTodo);
            }
        });
    }

    protected void onResume() {
        //在进行对数据库的数据更改后（添加 删除 更改），应该在主页面(MainActivity)中刷新数据，否则需要重新加载页面才能显示更改的数据
        super.onResume();
        refreshDataFromDB(); //重新更新数据
    }

    private void initEvent() {
        calendarView.setOnYearChangeListener(this);
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = new Date();
        nowDay = simpleDateFormat.format(date);
        mTodo = new ArrayList<>();
        nTodo = new ArrayList<>();
        mTodoDbOpenHelper = new TodoDbOpenHelper(this);
        mMainAdapter = new MainAdapter(this, mTodo); //调用构造器传入数据
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mMainAdapter);//为视图设置适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);//线性布局
        mRecyclerView.setLayoutManager(layoutManager);
        nTodo = mTodoDbOpenHelper.queryFromDbByDate(nowDay);

    }

    private void refreshDataFromDB() {
        mTodo = getDataFromDB(); //调用获取数据的方法来获取更新后的数据显示在主页面
        mMainAdapter.refreshData(mTodo);
        if (mTodo == null || mTodo.size() == 0) {
            tvClue.setVisibility(View.VISIBLE);
        }else {
            tvClue.setVisibility(View.GONE);
        }
    }

    private List<Todo> getDataFromDB() { //获取当天的todo
        return mTodoDbOpenHelper.queryFromDbByDate(nowDay);
    }


    //floatBtn的添加事件
    public void add(View view) {
        Intent intent = new Intent(this, AddActivity.class);
        startActivity(intent);
    }


    private void initView() {
        calendarView = findViewById(R.id.cv);
        mRecyclerView = findViewById(R.id.rv_todo);
        mCalendarLayout = findViewById(R.id.crl);
        tvClue = findViewById(R.id.tv_clue);
    }

    private String format(int date) {
        String exact_date;
        if (date <= 9) {
            exact_date = "0" + date;
        } else {
            exact_date = date + "";
        }
        return exact_date;
    }


    //菜单栏事件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_top, menu); //绑定菜单
        //绑定菜单搜索框
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) { //根据文本框中的字符进行查找
                mTodo = mTodoDbOpenHelper.queryFromDbByTitle(s);
                mMainAdapter.refreshData(mTodo);
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_switch:
                //转换视图
                Intent intent = new Intent(this, TodoListActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_count:
                //切换到统计视图
                Intent intent1 = new Intent(this, CountActivity.class);
                startActivity(intent1);
                break;
            case R.id.calendar_year:
                //进行年份选择
                calendarView.showYearSelectLayout(calendarView.getCurYear());
                setTitle(String.valueOf(calendarView.getCurYear()));
        }
        return super.onOptionsItemSelected(item);
    }

    public void switchCalendar(View view) {
        if (mCalendarLayout.isExpand()) {
            mCalendarLayout.shrink(360);
        } else {
            mCalendarLayout.expand(360);
        }
    }

    public void intentList(View view) {
        Intent intent = new Intent(this, TodoListActivity.class);
        startActivity(intent);
    }

    @Override
    public void onYearChange(int year) {
        setTitle(String.valueOf(year));
    }
}